<?php
/**
  * //w/w/w.s/h/u/o/t//u/p/u c/o/m  说 图/谱/源/码/分/享
 */
defined("IN_IA")or exit("Access Denied");
global $_W,$_GPC;
$op=$_GPC['op'] ? $_GPC['op'] :"index";
$uniacid=$_GPC['uniacid'];
$uid=$_GPC['uid'];

//订单列表
if($op=='orderList'){
	$request=array();
	$status=$_GPC['status'];		//1代付款 2待配送 3待收货 4全部订单
	if(!empty($_GPC['page'])){
		$page=intval($_GPC['page'])+1;
	}else{
		$page=1;
	}
	$condition=array();
	$condition['uid']=$uid;
	$condition['uniacid']=$uniacid;
	if($status==1){
		$condition['status']=0;
	}elseif($status==2){
		$condition['status']=1;
	}elseif($status==3){
		$condition['status']=2;
	}
	$condition['use_is_delete']=0;
	$orderData=pdo_getall('cqkundian_farm_shop_order',$condition,'','','create_time desc',array($page,10));

    $setData=pdo_get('cqkundian_farm_set',array('ikey'=>'expire_order_time','uniacid'=>$uniacid));

	for($n=0;$n<count($orderData);$n++){

        //判断订单支付时间是否过期
        if(!empty($setData) && $setData['value']!=0&& $setData['value']>0) {
            if($orderData[$n]['status']==0){
                if($orderData[$n]['create_time']+$setData['value'] <= time()){
                    pdo_update('cqkundian_farm_shop_order',array('status'=>5),array('uniacid'=>$uniacid,'id'=>$orderData[$n]['id']));
                }
            }
        }

        $order_detail = pdo_getall('cqkundian_farm_shop_order_detail', array('order_id' => $orderData[$n]['id'], 'uniacid' => $uniacid));
        if($orderData[$n]['order_type']==3){
            $specVal=pdo_get('cqkundian_farm_animal_sku',array('aid'=>$order_detail[0]['goods_id'],'uniacid'=>$uniacid));
            $spec_id=explode(",", $specVal['sku_name']);
            $spec_val=pdo_getall('cqkundian_farm_animal_spec_value',array('id in'=>$spec_id));
            $specItem=pdo_getall('cqkundian_farm_animal_spec',array('aid'=>$order_detail[0]['goods_id'],'uniacid'=>$uniacid));
            for($j=0;$j<count($specItem);$j++){
                for($m=0;$m<count($spec_val);$m++){
                    if($specItem[$j]['id']==$spec_val[$m]['spec_id']){
                        $specItem[$j]['spec_value']=$spec_val[$m]['spec_value'];
                    }
                }
            }
            $order_detail[0]['specItem']=$specItem;
        }elseif($orderData[$n]['order_type']==4){
            $sendData=pdo_get('cqkundian_farm_send',array('id'=>$order_detail[0]['goods_id']));
            $orderDetail[0]['goods_name']=$sendData['send_name'];
        }else {
            for ($i = 0; $i < count($order_detail); $i++) {
                $specVal = pdo_get('cqkundian_farm_goods_spec', array('goods_id' => $order_detail[$i]['goods_id'], 'uniacid' => $uniacid));
                $spec_id = explode(",", $specVal['sku_name']);
                $spec_val = pdo_getall('cqkundian_farm_spec_value', array('id in' => $spec_id));
                $specItem = pdo_getall('cqkundian_farm_spec', array('goods_id' => $order_detail[$i]['goods_id'], 'uniacid' => $uniacid));
                for ($j = 0; $j < count($specItem); $j++) {
                    for ($m = 0; $m < count($spec_val); $m++) {
                        if ($specItem[$j]['id'] == $spec_val[$m]['spec_id']) {
                            $specItem[$j]['spec_value'] = $spec_val[$m]['spec_value'];
                        }
                    }
                }
                $order_detail[$i]['specItem'] = $specItem;
            }
        }
    	$orderData[$n]['order_detail']=$order_detail;
	}
	$request['orderData']=$orderData;
	echo json_encode($request);die;
}

//取消订单
if($op=='cancelOrder'){
	$order_id=$_GPC['order_id'];
	$orderData=pdo_get('cqkundian_farm_shop_order',array('id'=>$order_id,'uniacid'=>$uniacid));
	//判断订单类型
	if($orderData['order_type']==0||$orderData['order_type']==3){
		$res=pdo_update('cqkundian_farm_shop_order',array('status'=>5),array('uniacid'=>$uniacid,'id'=>$order_id));
	}elseif($orderData['order_type']==1){	//组团商品订单
		$res=pdo_update('cqkundian_farm_shop_order',array('group_status'=>5),array('uniacid'=>$uniacid,'id'=>$order_id));
	}
	echo $res ? json_encode(array('code'=>1)):json_encode(array('code'=>2));die;
}

//申请退款
if($op=='applyRefund'){
	$order_id=$_GPC['order_id'];
	$orderData=pdo_get('cqkundian_farm_shop_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    $res=pdo_update('cqkundian_farm_shop_order',array('status'=>4),array('uniacid'=>$uniacid,'id'=>$order_id));
    if($res){
        include 'function.inc.php';
        $wxData=pdo_get('cqkundian_farm_wx_set',array('uniacid'=>$uniacid));
        $wx_openid=unserialize($wxData['get_openid']);
        for ($i=0;$i<count($wx_openid);$i++){
            $res_send_shop=cancelOrderSendTemplate($wx_openid[$i],$orderData,$uniacid);
        }
        echo json_encode(array('code'=>1));die;
    }else{
        echo json_encode(array('code'=>2));die;
    }
}

//确认收货
if($op=='sureGoods'){
	$order_id=$_GPC['order_id'];
	$orderData=pdo_get('cqkundian_farm_shop_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    //判断当前订单是否需要发放佣金
    $update_order=array(
        'status'=>3,
    );
    if($orderData['is_price']==1){
        include 'function.inc.php';
        $result=saleSendPirce($orderData,$uniacid,1);
        if($result){
            //修改佣金发放状态
            $update_order['is_price']=2;
        }
    }
    $res=pdo_update('cqkundian_farm_shop_order',$update_order,array('uniacid'=>$uniacid,'id'=>$order_id));

	echo $res ? json_encode(array('code'=>1)):json_encode(array('code'=>2));die;
}

//获取订单详细信息
if($op=='getOrderDetail'){
	$request=array();
	$order_id=$_GPC['order_id'];
	$orderData=pdo_get('cqkundian_farm_shop_order',array('id'=>$order_id,'uniacid'=>$uniacid));
    $order_detail=pdo_getall('cqkundian_farm_shop_order_detail',array('order_id'=>$order_id,'uniacid'=>$uniacid));
    //查询商品规格
    for($i=0;$i<count($order_detail);$i++){
        $specVal=pdo_get('cqkundian_farm_goods_spec',array('goods_id'=>$order_detail[$i]['goods_id'],'uniacid'=>$uniacid));
        $spec_id=explode(",", $specVal['sku_name']);
        $spec_val=pdo_getall('cqkundian_farm_spec_value',array('id in'=>$spec_id));
        $specItem=pdo_getall('cqkundian_farm_spec',array('goods_id'=>$order_detail[$i]['goods_id'],'uniacid'=>$uniacid));
        for($j=0;$j<count($specItem);$j++){
            for($m=0;$m<count($spec_val);$m++){
                if($specItem[$j]['id']==$spec_val[$m]['spec_id']){
                    $specItem[$j]['spec_value']=$spec_val[$m]['spec_value'];
                }
            }
        }
        $order_detail[$i]['specItem']=$specItem;
    }
    $orderData['create_time']=date("Y-m-d H:i:s",$orderData['create_time']);
    if($orderData['pay_time']){
    	$orderData['pay_time']=date("Y-m-d H:i:s",$orderData['pay_time']);
    }else{
    	$orderData['pay_time']='--';
    }
    $request['orderData']=$orderData;
    $request['orderDetail']=$order_detail;
    //运费
    $aboutData=pdo_get('cqkundian_farm_about',array('uniacid'=>$uniacid));
    $request['aboutData']=$aboutData;
    echo json_encode($request);die;
}

//删除订单
if($op=='deleteOrder'){
    $orderid=$_GPC['orderid'];
    $uid=$_GPC['uid'];
    $res=pdo_update('cqkundian_farm_shop_order',array('use_is_delete'=>1),array('uniacid'=>$uniacid,'uid'=>$uid,'id'=>$orderid));
    echo $res  ? json_encode(array('code'=>1,'msg'=>'删除成功')) : json_encode(array('code'=>2,'msg'=>'删除失败'));die;
}





